package com.kkwrite.regimen.consumer.service.user;

import com.kkwrite.regimen.common.entity.dto.user.RegUserInDTO;
import com.kkwrite.regimen.common.entity.dto.user.RegUserOutDTO;
import com.kkwrite.regimen.consumer.controller.constant.Constant;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Created by lisha on 2018/7/16 16:16.
 *
 * @author lisha
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource(name = "stringRedisTemplate")
    private StringRedisTemplate redisTemplate;
    @Resource(name = "restTemplate")
    private RestTemplate restTemplate;

    @Override
    public RegUserOutDTO updateUser(RegUserInDTO inDTO, String token) {
        if (inDTO.isEmpty()) {
            return new RegUserOutDTO("true", "执行成功");
        }
        Object userIdObj = redisTemplate.opsForHash().get(token, "userId");
        if (userIdObj == null) {
            return new RegUserOutDTO("false", "002");
        }
        inDTO.setUserId(Integer.valueOf((String) userIdObj));
        return restTemplate.postForObject(Constant.SERVICE_PREFIX + "/user/update", inDTO, RegUserOutDTO.class);
    }
}
